<?php
/*
	Version   : 1.3
	Created   : 20081002
	Modified  : 20090927
	Author    : Pablo Larenas
	co-Author : Pablo Ramirez
	Descr     : Controlador "Service Types"
	Changelog : 1.0 Version Inicial
				1.1 Se agrega relacion con survey_type
				1.2 Se agrega campo applicable_to
				1.3 Control de Acceso
*/
class ServiceTypesController extends AppController
{
	var $name      = 'ServiceTypes';
	var $helpers    = array('Html','Javascript','Ajax');
	var $uses       = array('ServiceType','SurveyType');
	
	function index() {
		$this->checkSession('admin');
		$this->layout = 'maintenance';
		$setArray = array();
		
		if (empty($this->data)) {
			$sets = $this->ServiceType->findAll(null,'*','set_codigo ASC');
			foreach($sets as $set) {
				$temp = array();
				foreach($set['ServiceType'] as $key=>$value) {
					$temp[$key] = $value;
				}
				$setArray[] = $temp;
			}
			$this->set('serviceTypes', $setArray);
		} 
	}
	
	function add($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		if (!empty($this->data)){
			$this->id = null;
			if ($this->ServiceType->save($this->data)){
				$this->redirect('/Service_types'); 
			}
		} else {
			$this->set('id', $id);
			$this->set('survey_types', $this->SurveyType->generateList());
		}
	}
	
	function edit($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->ServiceType->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->ServiceType->read();
			$this->set('id', $id);
			$this->set('survey_types', $this->SurveyType->generateList());
		}else{
			if ($this->ServiceType->save($this->data)){
				//$this->Session->setFlash('Item Editado');
				$this->redirect('/Service_types');
			} else {
				$this->Session->setFlash('Error al editar el Item');
			}
		}
	}
	
	function view($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->ServiceType->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->ServiceType->read();
			$this->set('id', $id);
			$this->set('survey_types', $this->SurveyType->generateList());
		}
	}
	
	function delete($id) {
		$this->checkSession('admin');
		if ($this->ServiceType->del($id)) { 
			$this->set('error', '0');
		} else {
			$this->set('error','1');
		}
		$this->redirect('/Service_types');
	
	}
}
?>